//
//  SinaMainTabBarVC.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/12.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit

class SinaMainTabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //1.自定义tabbar
        let mytabBar = SinaTabBar()
        self.setValue(mytabBar, forKey:"tabBar")//只读属性,不能直接赋值,只能通过KVC
        //1.2监听➕按钮的点击事件
        mytabBar.composeClosure = {[weak self] in
            
            //print("➕按钮被点击了")
            //显示
            let composeView = SinaComposeView()
            UIApplication.shared.keyWindow?.addSubview(composeView)
        }
       
        
        //2.添加子控制器
       addChildViewController()
       
        
        
        
    }
    //MARK: -
    //MARK: - 添加子控制器
    private func addChildViewController() {
        
        addChildViewControllers(vc: SinaHomeTableVC(), title: "首页", imagename: "tabbar_home",index:0)
        addChildViewControllers(vc: SinaMessageTableVC(), title: "消息", imagename: "tabbar_message_center",index:1)
        addChildViewControllers(vc: SinaDiscoverTableVC(), title: "发现", imagename: "tabbar_discover",index:2)
        addChildViewControllers(vc: SinaProfileTableVC(), title: "我的", imagename: "tabbar_profile",index:3)
    }
    //MARK: -
    //MARK: - 创建子控制器并包装Nav
    private func addChildViewControllers(vc:UIViewController,title:String,imagename:String,index:Int){

        
        //设置TabBarItem
        //图片
        vc.tabBarItem.image = UIImage(named: imagename)
        vc.tabBarItem.selectedImage = UIImage(named:imagename+"_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //文字
        vc.tabBarItem.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName :UIColor.orange], for: UIControlState.selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: UIControlState.normal)
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 0)//Item中图片与文字的距离
        //徽章
        vc.tabBarItem.badgeValue = "10"
        vc.tabBarItem.badgeValue = nil
        vc.tabBarItem.badgeColor = #colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)
        //tag
        vc.tabBarItem.tag = index
        //包装一层导航控制器
        let nav = SinaBaseNavVCViewController(rootViewController: vc)
        
        vc.navigationItem.title = title
        //将导航控制器添加至当前UITabBarController的子视图控制器
        addChildViewController(nav)
        
    }
   
    //MARK: -
    //MARK: - tabBarItem被点击事件
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        //print(item.tag)
        //1.应该获取到被点击的UITabBarButton
        var index = 0
        for subView in tabBar.subviews {
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                if index == item.tag {
                    //可以获取到被点击tabbarButton
                    //遍历子视图
                    for v in subView.subviews {
                        if v.isKind(of: NSClassFromString("UITabBarSwappableImageView")!) {
                            //print("终于找到你,还好没放弃\(v)")
                            //线缩小
                            v.transform = CGAffineTransform.init(scaleX: 0.6, y: 0.6)
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 6, options: [], animations: {
                                v.transform = CGAffineTransform.identity
                                }, completion: { (_) in
                                    //print("👌")
                            })
                        }
                    }
                }
                index += 1
            }
        }
    }

    
    
    
    
    
}
